package com.qf.service.impl;

import com.qf.dao.UserDao;
import com.qf.pojo.User;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/29 11:37
 */
@Service
//开启事务的注解,此类所有方法都开启事务,属性都是默认的
@Transactional
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User findById(long id) {
        return userDao.findById(id);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }
//加扎起方法上，代表此方法开启事务,优先级高于全局事务
//                  传播行为                                回滚条件                    只读
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class,readOnly = true)
    @Override
    public int AddUser(User user) {
        int i = userDao.AddUser(user);
        int a=10/0;
        int b = userDao.AddUser(user);
        return i+b;
    }

}
